/*
 * 文章
 */

import { createAPI } from '@/utils/request'
import * as URL from '../../constants/api'
const formOfEmploymentMapping = new Map([
  [1, '已启用'],
  [0, '已禁用'],
  ['1', '启用'],
  [2, '禁用']
])
// 文章列表
export const artlcesList = data => createAPI(URL.ARTICLES, 'get', data)
.then(res => ({
  items: res.data.items.map((p, i) => ({
    ...p,
    index: ++i,
    state: formOfEmploymentMapping.get(p.state)
  })),
  counts: res.data.counts,
  page: res.data.page,
  pages: res.data.pages,
  pagesize: res.data.pagesize
}))
.catch(() => {})
// 文章简单列表
export const simple = data => createAPI('/articles/simple', 'get', data)

// 文章详情
export const detail = data => createAPI(`/articles/${data.id}`, 'get', data)

// 文章添加
export const addArticles = data => createAPI(URL.ARTICLES, 'post', data)
.then(res => res)
.catch(() => {})
// 文章修改
export const update = data => createAPI(`/articles/${data.id}`, 'put', data)

// 文章删除
export const remove = data => createAPI(`${URL.ARTICLES}/${data.id}`, 'delete', data)
.then(res => res)
.catch(() => {})
// 文章状态
export const changeState = data =>
  createAPI(`/articles/${data.id}/${data.state}`, 'post', data)
